package cn.tedu.carehome.foundation.service.impl;
import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.FoodPackageLinkMapper;
import cn.tedu.carehome.foundation.pojo.dto.FoodPackageLinkAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.FoodPackageLinkUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.FoodPackageLink;
import cn.tedu.carehome.foundation.pojo.vo.FoodPackageLinkAdminVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodPackageLinkUpdateVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.service.IFoodPackageLinkService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Slf4j
@Service
public class FoodPackageLinkServiceImpl implements IFoodPackageLinkService {
    @Autowired
    private FoodPackageLinkMapper foodPackageLinkMapper;

    @Override
    public void addNew(FoodPackageLinkAddNewDTO foodPackageLinkAddNewDTO){
        log.debug("开始处理【添加套餐餐品关联】的业务，参数：{}", foodPackageLinkAddNewDTO);
        FoodPackageLink foodPackageLink=new FoodPackageLink();
        BeanUtils.copyProperties(foodPackageLinkAddNewDTO,foodPackageLink);
        log.debug("即将插入数据，参数：{}", foodPackageLink);
        foodPackageLinkMapper.insert(foodPackageLink);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除套餐】的业务，编号参数：{}", id);
        int countById= foodPackageLinkMapper.countById(id);
        if(countById==0){
            String message = "删除套餐餐品关联失败,套餐餐品关联id不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "套餐删除失败，尝试删除的相册不存在！");
        }
        foodPackageLinkMapper.deleteById(id);


    }

    @Override
    public List<FoodPackageLinkAdminVO> queryFoodPackageLinkList() {
        log.debug("开始处理【查询套餐列表】的业务，无参数");
        return foodPackageLinkMapper.selectAdmin();
    }

    @Override
    public JsonPage<FoodPackageLinkAdminVO> queryFoodPackageLinkListByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<FoodPackageLinkAdminVO> list = foodPackageLinkMapper.selectAdminByPage();
        return JsonPage.restPage(new PageInfo<>(list));
    }

    @Override
    public FoodPackageLinkUpdateVO selectById(Long id) {
        log.debug("开始处理【根据套餐餐品关联id查询套餐餐品关联】的业务，参数id{}",id);
        return foodPackageLinkMapper.selectById(id);
    }

    @Override
    public void update(FoodPackageLinkUpdateDTO foodPackageLinkUpdateDTO) {
        log.debug(String.valueOf(foodPackageLinkUpdateDTO));
        log.debug("开始处理【根据套餐餐品id修改套餐餐品关联】的业务，参数{}",foodPackageLinkUpdateDTO);
        // 检查餐品id是否存在

        int countById= foodPackageLinkMapper.countById(foodPackageLinkUpdateDTO.getId());
        if(countById==0){
            String message = "修改套餐失败,关联物id不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "修改套餐餐品失败，尝试修改的关联物不存在！");
        }
        foodPackageLinkMapper.update(foodPackageLinkUpdateDTO);
    }



}
